package com.hdoit.ares.web.acti.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acti.model.ActiTask;
import com.hdoit.ares.logical.acti.service.IActiTaskService;
import com.hdoit.ares.logical.viewmodel.acti.ActiTaskSearch;
import com.hdoit.ares.selfcommon.enums.acti.ActiState;
import com.hdoit.ares.selfcommon.enums.brokerage.ConfirmState;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      caochengmeng
 * @email       
 * @createtime  2018年03月14日 08:52:27
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/acti/actiTask/")
public class ActiTaskController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/acti/actiTask/";
    }

    private IActiTaskService actiTaskService;
    public IActiTaskService getActiTaskService() {
        return actiTaskService;
    }
    @Resource
    public void setActiTaskService(IActiTaskService actiTaskService) {
        this.actiTaskService = actiTaskService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="actiTaskDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String toConfirmViewName = "toConfirm";
    

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,ActiTaskSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(actiTaskService.queryCount(search));
        List<ActiTask> actiTasks= actiTaskService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("actiTasks", actiTasks);
        model.addAttribute("actiTaskSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String actiTaskDetailed(Model model,String key){
        ActiTask actiTask=new ActiTask();
        if(!StringUtils.isEmpty(key)){
            actiTask=actiTaskService.queryModel(key);
        }
        model.addAttribute("actiTask", actiTask);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,ActiTask actiTask){
        if(StringUtils.isEmpty(actiTask.getId())){
            actiTask.setId(CommonUtils.getKeyId());
            actiTask.setCreateDate(DateUtils.getCurrent());
            actiTask.setDeleteMark("1");
            actiTask.setCreateUserId(getLoginModel().getLoginId());
            actiTask.setCreateUserName(getLoginModel().getLoginName());
            actiTask.setConfirmState(ConfirmState.unConfirm);
            actiTask.setTaskState(ActiState.down);
            actiTask.setModelStatus(ModelStatus.add);
        }else{
            actiTask.setModifyDate(DateUtils.getCurrent());
            actiTask.setModifyUserId(getLoginModel().getLoginId());
            actiTask.setModifyUserName(getLoginModel().getLoginName());
            actiTask.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(actiTaskService.applyChange(actiTask)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("actiTask", actiTask);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(actiTaskService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    
    @RequestMapping(value =toConfirmViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    @ResponseBody 
    public String toConfirm(Model model,String id){
    		
    	ActiTask actiTask = actiTaskService.queryModel(id);

    	actiTask.setTaskState(ActiState.up);
    	actiTask.setConfirmState(ConfirmState.confirmed);
    	actiTask.setModelStatus(ModelStatus.update);
    	if(!actiTaskService.applyChange(actiTask)){
    		return new JsonActionResult(ManageContainer.ERROR).toJson();
    	}
    	return new JsonActionResult(ManageContainer.SUCCESS).toJson();
	
    }
}